const util = require('../util')

const test1 = () => {
  let str = ' create mode 100644 "docs/\\345\\237\\272\\347\\241\\200\\345\\271\\263\\345\\217\\260/\\346\\234\\215\\345\\212\\241\\345\\231\\250\\347\\273\\264\\346\\212\\244/tomcat\\346\\227\\240\\346\\263\\225\\345\\201\\234\\346\\255\\242.md"'

  let s = '';

  for (let i=0, len=str.length; i<len; i++) {
    if (str[i] === '\\') {
      let tmp = str.substr(i + 1, 11).split('\\')
      s += Buffer.from([parseInt(Number(tmp[0]), 8), parseInt(Number(tmp[1]), 8), parseInt(Number(tmp[2]), 8)])
      i += 11
    } else {
      // buf.push(str[i].charCodeAt())
      s += str[i]
    }
  }

  // buf = [parseInt(345, 8), parseInt(237, 8), parseInt(272, 8)]
  // console.log(Buffer.from(buf).toString())
  console.log(s)
}

const number2String = (numberString) => {
  let numberArray = numberString.split('\\')
  let numbers = []
  for (let n of numberArray) {
    n && numbers.push(parseInt(Number(n), 8))
  }
  let buf = ''
  for (let i = numbers.length - 1; i>=0; i -= 3) {
    if (i >= 2) {
      buf = Buffer.from([numbers[i-2], numbers[i-1], numbers[i]]) + buf
    }
  }
  return buf;
}

const test2 = (str) => {

  let reg = /(\\[0-9]{3})+/g

  let buf = ''
  let begin = 0, end = str.length;
  let match = reg.exec(str)
  while(match){
    buf += str.substr(begin, match.index - begin)
    buf += number2String(match[0])
    begin = match.index + match[0].length
    match = reg.exec(str);
  }
  buf += str.substr(begin, end)
  return buf
};


let str = `
Updating cb5556d..1428ee9
Fast-forward
 .../index.md"                                      |   2 -
 .../index.md"                                      |  10 +-
 ...256\\232\\346\\227\\266\\347\\273\\237\\350\\256\\241.md" | 109 +++++++++++
 ...273\\237\\350\\256\\241\\346\\241\\206\\346\\236\\266.md" | 209 +++++++++++++++++++++
 4 files changed, 327 insertions(+), 3 deletions(-)
 create mode 100644 "docs/\\351\\203\\221\\345\\267\\236\\344\\272\\244\\350\\255\\246\\346\\224\\257\\351\\230\\237/\\346\\225\\260\\346\\215\\256\\345\\244\\251\\345\\271\\263/\\345\\256\\232\\346\\227\\266\\347\\273\\237\\350\\256\\241.md"
 create mode 100644 "docs/\\351\\203\\221\\345\\267\\236\\344\\272\\244\\350\\255\\246\\346\\224\\257\\351\\230\\237/\\346\\225\\260\\346\\215\\256\\345\\244\\251\\345\\271\\263/\\347\\273\\237\\350\\256\\241\\346\\241\\206\\346\\236\\266.md"
`
console.log(util.out2String(str))

// console.log(number2String('256\\232\\346\\227\\266\\347\\273\\237\\350\\256\\241'));
// console.log(test2(str))